package com.ldn.dao.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ldn.basic.enums.CachePrefixEnums;
import com.ldn.basic.utils.DataUtil;
import com.ldn.basic.utils.DateUtils;
import com.ldn.basic.utils.NetWorkUtil;
import com.ldn.basic.utils.cache.HutoolCacheUtils;
import com.ldn.dao.entities.BasicEntities;
import com.ldn.dao.service.BasicService;

import java.io.Serializable;
import java.util.List;

/**
 * @author 李大牛家的牛 (scafel@163.com)
 * @createDate 2024/7/11 - 14:43
 * @description ...
 **/
@SuppressWarnings("unused")
public class BasicImpl<M extends BaseMapper<T>, T extends BasicEntities> extends ServiceImpl<M, T> implements BasicService<T> {
    public boolean saveEntity(T entity) {
        return super.save(setAddField(entity));
    }

    public boolean saveBatchEntity(List<T> entities) {
        entities.forEach(this::setAddField);
        return super.saveBatch(entities);
    }

    public boolean updateBatchEntity(List<T> entities) {
        entities.forEach(this::setBaseField);
        return super.updateBatch(entities);
    }

    public boolean updateEntity(T entity) {
        return super.updateById(setBaseField(entity));
    }

    public boolean deleteEntity(T entity) {
        super.updateById(setDeleteField(entity));
        return super.removeById(entity);
    }

    public boolean deleteBatchEntity(List<? extends Serializable> ids) {
        return super.removeByIds(ids);
    }

    public boolean statusEntity(T entity) {
        return super.updateById(setStatusField(entity));
    }

    public boolean hotEntity(T entity) {
        return super.updateById(setHotField(entity));
    }

    public boolean protectedEntity(T entity) {
        return super.updateById(setProtectedField(entity));
    }


    public Page<T> pageEntity(Integer page, Integer size) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.orderBy("id", false);
        return super.page(Page.of(page, size), queryWrapper);
    }

    public Page<T> pageEntity(Integer page) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.orderBy("id", false);
        return super.page(Page.of(page, 20), queryWrapper);
    }

    public Page<T> pageStatusEntity(Integer page, Integer size) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.eq("status", 1);
        queryWrapper.orderBy("id", false);
        return super.page(Page.of(page, size), queryWrapper);
    }

    public Page<T> pageStatusEntity(Integer page) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.eq("status", 1);
        queryWrapper.orderBy("id", false);
        return super.page(Page.of(page, 20), queryWrapper);
    }

    public List<T> listEntity() {
        return super.list();
    }

    public List<T> listStatusEntity() {
        return super.list(QueryWrapper.create().eq("status", 1));
    }

    public List<T> listEntity(Integer page) {
        return super.page(Page.of(page, 20)).getRecords();
    }

    public List<T> listStatusEntity(Integer page) {
        return super.page(Page.of(page, 20), QueryWrapper.create().eq("status", 1)).getRecords();
    }

    public List<T> getListByTimeForMember(Serializable memberId, long startTime, long endTime){
        QueryWrapper query = QueryWrapper.create();
        query.eq("w_m_u_id", memberId);
        query.between("c_time", startTime, endTime);
        return super.list(query);
    }

    protected <E extends BasicEntities>E setAddField(E entity) {
        String loginId = "-1";
        try {
            loginId = StpUtil.getLoginId("-1");
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        entity.setCtime(DateUtils.getCurrentS());
        // 获取当前登录用户的信息
//        if (entity.getStatus() == 1){
//            entity.setStatus(1);
//        } else {
//            entity.setStatus(0);
//        }
//        if (user != null){
//            entity.setCauId(user.getId());
//        }else {
//            entity.setCauId(-1);
//        }
//        if (memberUser != null){
//            entity.setCmuId(memberUser.getId());
//        }else {
//            entity.setCmuId(-1);
//        }
        entity = setBaseField(entity);
        return entity;
    }

    protected <E extends BasicEntities>E setHotField(E entity) {
        if (entity.getIsHot() == 1) {
            entity.setIsHot(0);
        } else {
            entity.setIsHot(1);
        }
        entity = setBaseField(entity);
        return entity;
    }

    protected <E extends BasicEntities>E setProtectedField(E entity) {
        if (entity.getIsProtected() == 1) {
            entity.setIsProtected(0);
        } else {
            entity.setIsProtected(1);
        }
        entity = setBaseField(entity);
        return entity;
    }

    protected <E extends BasicEntities>E setStatusField(E entity) {
        if (entity.getStatus() == 1) {
            entity.setStatus(0);
        } else {
            entity.setStatus(1);
        }
        entity = setBaseField(entity);
        return entity;
    }

    protected <E extends BasicEntities>E setDeleteField(E entity) {
        entity.setIsDelete(1);
        entity.setDtime(DateUtils.getCurrentS());
        entity = setBaseField(entity);
        return entity;
    }

    protected <E extends BasicEntities>E setBaseField(E entity) {
        String loginId = "-1";
        try {
            loginId = StpUtil.getLoginId("-1");
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        // 获取当前登录用户的信息
//        if (user != null){
//            entity.setUauId(user.getId());
//        }else {
//            entity.setUauId(-1);
//        }
//        if (memberUser != null){
//            entity.setUmuId(memberUser.getId());
//        }else {
//            entity.setUmuId(-1);
//        }
        entity = setField(entity);
        return entity;
    }

    protected <E extends BasicEntities>E setField(E entity) {
        entity.setUtime(DateUtils.getCurrentS());
        entity.setUOs(NetWorkUtil.getOsName());
        entity.setUIp(NetWorkUtil.getIpAddr());
        return entity;
    }


}
